package com.syt.tank.message;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.mina.core.session.IoSession;

import com.syt.mina.message.IMessage;
import com.syt.mina.session.AbstractSessionManager;
import com.syt.mina.session.DefaultSessionManager;

import flex.messaging.io.amf.ASObject;

public class TankSessionManager extends AbstractSessionManager{
	public static final String REQUEST_ID_FLAG = "_syt_request_id_flag";
	
	private Map<String,AddPlayerMsg> playerMap = Collections.synchronizedMap(new HashMap<String,AddPlayerMsg>());
	
	
	
	@Override
	public void addSession(IoSession session){
		String id = String.valueOf(session.getId());
		sessionMap.put(id, session);
		AddPlayerMsg addPlayerMsg = new AddPlayerMsg(id,null);
		playerMap.put(id, addPlayerMsg);
		ASObject as = new ASObject();
		as.put("msgId", "0000");
		as.put("playerId", id);
		session.write(as);
	}
	
	@Override
	public void deleteSession(IoSession session){
		String id = String.valueOf(session.getId());
		sendRemovePlayerMsg(id);
	}
	

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception{
		String id = String.valueOf(session.getId());
		session.close(true);
		sendRemovePlayerMsg(id);
	}
	
	@Override
	public void sendMessageTo(IoSession session, IMessage message) {
		String requestId = (String)session.getAttribute(TankSessionManager.REQUEST_ID_FLAG);
		if(requestId != null){
			((AbstractMessage)message).setRequestId(requestId);
		}
		
		if(session!=null && session.isConnected()){
			session.write(message.getMessage());
		}
	}
	
	public void sendRemovePlayerMsg(String id) {
		sessionMap.remove(id);
		playerMap.remove(id);
		
		ASObject as = new ASObject();
		as.put("msgId", "1001");
		as.put("playerId", id);
		for(IoSession session : sessionMap.values()){
			if(session.isConnected() && ! session.isWriterIdle()){
				session.write(as);
			}
		}
	}
	
	
	@Override
	public boolean isDispach(IoSession session, Object message) {
		if(message != null && message instanceof ASObject){
			ASObject as = (ASObject)message;
			String msgId = (String)as.get("key");
			String requestId = (String)as.get("requestId");
			if(msgId != null){
				if ("/requestSvrTimeMsg".equals(msgId)){
					as = new ASObject();
					as.put("msgId", "00000");
					as.put("s_recv_time", String.valueOf(System.currentTimeMillis()));
					as.put("s_send_time", String.valueOf(System.currentTimeMillis()));
					as.put("requestId", requestId);
					session.write(as);
					return false;
				}
				
				if(requestId != null){
					session.setAttribute(TankSessionManager.REQUEST_ID_FLAG,requestId);
				}
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	
	
	public void updateInfo(String id,Integer x, Integer y, boolean isMove){
		AddPlayerMsg addPlayerMsg = playerMap.get(id);
		if(addPlayerMsg != null){
			addPlayerMsg.setX(x);
			addPlayerMsg.setY(y);
			addPlayerMsg.setMove(isMove);
		}
	}
	
	
	public Map<String, AddPlayerMsg> getPlayerMap() {
		return playerMap;
	}

	public void initRunTask() {
		// TODO Auto-generated method stub
		new Thread(new runT()).start();
	}
	
	
	private class runT implements Runnable{

		@Override
		public void run() {
			
			
			for(;;){
				try {
					Thread.sleep(100);
					
					doWork();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		private void doWork(){
			for(AddPlayerMsg m : playerMap.values()){
				if(m.isMove()){
					ASObject as = new ASObject();
					as.put("msgId", "1111");
					as.put("playerId", m.getPlayerFrom());
					as.put("time_stamp", System.currentTimeMillis());
					for(IoSession session : sessionMap.values()){
						if(session.isConnected() && ! session.isWriterIdle()){
							session.write(as);
						}
					}
				}
			}
		}
	}


}
